home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / LB09D.ZIP / HANOI.BAS < prev    next >
BASIC Source File  |  1993-08-10  |  7KB  |  273 lines

  1.  
  2.     ' Hanoi.bas, copyright 1993, Shoptalk Systems
  3.  
  4.     ' This simple Liberty BASIC program let's the user
  5.     ' play the puzzle "Tower of Hanoi".
  6.  
  7.     ' Instructions:
  8.     '
  9.     ' Move all the disks from Peg 1 to Peg 3, one disk at a
  10.     ' time, by taking each disk from one peg and placing it onto
  11.     ' another peg.  You can only take one disk at a time, and
  12.     ' only the top disk may be removed.  When placing a
  13.     ' disk, it can only be placed onto an unoccupied peg or
  14.     ' onto a peg occupied with larger disks.  You cannot place
  15.     ' a larger disk onto a smaller one.
  16.     '
  17.     ' By shuffling the disks around, it is possible to complete
  18.     ' the puzzle.  When all the disks are finally moved to peg 3,
  19.     ' you will be given the chance to play again.
  20.     '
  21.  
  22.  
  23.     ' Prepare the variables that hold the disk data
  24.     tiny = 1
  25.     small = 1
  26.     medium = 1
  27.     large = 1
  28.  
  29.     ' Set the movement state
  30.     state$ = "begin"
  31.  
  32.     gosub [openWindow]
  33.     gosub [drawPuzzle]
  34.  
  35.  
  36. [inputLoop]
  37.  
  38.     if hanoiStarted = 0 then print #hanoi, "redraw" ; : hanoiStarted = 1
  39.     input r$
  40.     goto [inputLoop]
  41.  
  42.  
  43.  
  44.  
  45. [openWindow]    'Open the puzzle's window
  46.  
  47.     WindowWidth = 430
  48.     WindowHeight = 320
  49.  
  50.     button #hanoi, " Peg 1 ", [pegOne], UL, 70, 240
  51.     button #hanoi, " Peg 2 ", [pegTwo], UL, 170, 240
  52.     button #hanoi, " Peg 3 ", [pegThree], UL, 270, 240
  53.     open "Tower of Hanoi" for graphics as #hanoi
  54.  
  55.     return
  56.  
  57.  
  58. [drawPuzzle]    ' Draw the pegs
  59.  
  60.     print #hanoi, "cls ; down" ;
  61.     print #hanoi, "color darkgray ; size 10 ; line 100 120 100 220" ;
  62.     print #hanoi, "line 200 120 200 220" ;
  63.     print #hanoi, "line 300 120 300 220" ;
  64.     print #hanoi, "color lightgray ; size 15 ; line 0 220 400 220" ;
  65.  
  66.  
  67. [drawDisks]     ' Draw the disks
  68.  
  69.     peg1 = 207
  70.     peg2 = 207
  71.     peg3 = 207
  72.  
  73.     if large <> 1 then [largeIsNotOne]
  74.     print #hanoi, "size 10 ; color red" ;
  75.     print #hanoi, "line 60 "; peg1; " 140 "; peg1 ;
  76.     peg1 = peg1 - 10
  77.     goto [drawMedium]
  78.  
  79. [largeIsNotOne]
  80.  
  81.     if large <> 2 then [largeIsNotTwo]
  82.     print #hanoi, "size 10 ; color red" ;
  83.     print #hanoi, "line 160 "; peg2; " 240 "; peg2 ;
  84.     peg2 = peg2 - 10
  85.     goto [drawMedium]
  86.  
  87. [largeIsNotTwo]
  88.  
  89.     print #hanoi, "size 10 ; color red" ;
  90.     print #hanoi, "line 260 "; peg3; " 340 "; peg3 ;
  91.     peg3 = peg3 - 10
  92.  
  93.  
  94. [drawMedium]    ' Draw the medium size disk
  95.  
  96.     if medium <> 1 then [mediumIsNotOne]
  97.     print #hanoi, "size 10 ; color green" ;
  98.     print #hanoi, "line 70 "; peg1; " 130 "; peg1 ;
  99.     peg1 = peg1 - 10
  100.     goto [drawSmall]
  101.  
  102. [mediumIsNotOne]
  103.  
  104.     if medium <> 2 then [mediumIsNotTwo]
  105.     print #hanoi, "size 10 ; color green" ;
  106.     print #hanoi, "line 170 "; peg2; " 230 "; peg2 ;
  107.     peg2 = peg2 - 10
  108.     goto [drawSmall]
  109.  
  110. [mediumIsNotTwo]
  111.  
  112.     print #hanoi, "size 10 ; color green" ;
  113.     print #hanoi, "line 270 "; peg3; " 330 "; peg3 ;
  114.     peg3 = peg3 - 10
  115.  
  116. [drawSmall]    ' Draw the small size disk
  117.  
  118.     if small <> 1 then [smallIsNotOne]
  119.     print #hanoi, "size 10 ; color blue" ;
  120.     print #hanoi, "line 80 "; peg1; " 120 "; peg1 ;
  121.     peg1 = peg1 - 10
  122.     goto [drawTiny]
  123.  
  124. [smallIsNotOne]
  125.  
  126.     if small <> 2 then [smallIsNotTwo]
  127.     print #hanoi, "size 10 ; color blue" ;
  128.     print #hanoi, "line 180 "; peg2; " 220 "; peg2 ;
  129.     peg2 = peg2 - 10
  130.     goto [drawTiny]
  131.  
  132. [smallIsNotTwo]
  133.  
  134.     print #hanoi, "size 10 ; color blue" ;
  135.     print #hanoi, "line 280 "; peg3; " 320 "; peg3 ;
  136.     peg3 = peg3 - 10
  137.  
  138.  
  139. [drawTiny]    ' Draw the tiny size disk
  140.  
  141.     if tiny <> 1 then [tinyIsNotOne]
  142.     print #hanoi, "size 10 ; color yellow" ;
  143.     print #hanoi, "line 90 "; peg1; " 110 "; peg1 ;
  144.     peg1 = peg1 - 10
  145.     goto [finishDrawing]
  146.  
  147. [tinyIsNotOne]
  148.  
  149.     if tiny <> 2 then [tinyIsNotTwo]
  150.     print #hanoi, "size 10 ; color yellow" ;
  151.     print #hanoi, "line 190 "; peg2; " 210 "; peg2 ;
  152.     peg2 = peg2 - 10
  153.     goto [finishDrawing]
  154.  
  155. [tinyIsNotTwo]
  156.  
  157.     print #hanoi, "size 10 ; color yellow" ;
  158.     print #hanoi, "line 290 "; peg3; " 310 "; peg3 ;
  159.     peg3 = peg3 - 10
  160.  
  161.  
  162. [finishDrawing]
  163.  
  164.     print #hanoi, "flush" ;
  165.  
  166.     return
  167.  
  168.  
  169.  
  170. [pegOne]
  171.  
  172.    pegInQuestion = 1
  173.    if state$ = "begin" then [beginMove] else [endMove]
  174.  
  175. [pegTwo]
  176.  
  177.    pegInQuestion = 2
  178.    if state$ = "begin" then [beginMove] else [endMove]
  179.  
  180. [pegThree]
  181.  
  182.    pegInQuestion = 3
  183.    if state$ = "begin" then [beginMove] else [endMove]
  184.  
  185.  
  186. [beginMove]     ' Start to move a disk
  187.  
  188.     peg = pegInQuestion
  189.  
  190.     noDisks$ = "There are no disk on that peg!"
  191.     if tiny <> peg and small <> peg and medium <> peg and large <> peg then notice noDisks$ : goto [inputLoop]
  192.  
  193.     print #hanoi, "place 10 10 ; color black " ;
  194.     print #hanoi, "\\ Peg "; peg; " has been selected.\ Please select a destination peg." ;
  195.  
  196.     state$ = "end"
  197.  
  198.     goto [inputLoop]
  199.  
  200.  
  201. [endMove]   ' Finish moving a disk
  202.  
  203.     state$ = "begin"
  204.     ontoPeg = pegInQuestion
  205.     if ontoPeg = peg then [resetSelection]
  206.  
  207.     ' Determine which disk to move
  208.     if large = peg then disk$ = "large"
  209.     if medium = peg then disk$ = "medium"
  210.     if small = peg then disk$ = "small"
  211.     if tiny = peg then disk$ = "tiny"
  212.  
  213.     ' Determine if move is legal
  214.     moveOnto$ = "nothing"
  215.     if large = ontoPeg then moveOnto$ = "large"
  216.     if medium = ontoPeg then moveOnto$ = "medium"
  217.     if small = ontoPeg then moveOnto$ = "small"
  218.     if tiny = ontoPeg then moveOnto$ = "tiny"
  219.  
  220.     if moveOnto$ = "nothing" then [finishMove]
  221.  
  222.     if disk$ = "large" and moveOnto$ = "medium" then [illegalMove]
  223.     if disk$ = "large" and moveOnto$ = "small" then [illegalMove]
  224.     if disk$ = "medium" and moveOnto$ = "small" then [illegalMove]
  225.     if disk$ = "medium" and moveOnto$ = "tiny" then [illegalMove]
  226.     if disk$ = "small" and moveOnto$ = "tiny" then [illegalMove]
  227.  
  228. [finishMove]
  229.  
  230.     if disk$ = "large" then large = ontoPeg
  231.     if disk$ = "medium" then medium = ontoPeg
  232.     if disk$ = "small" then small = ontoPeg
  233.     if disk$ = "tiny" then tiny = ontoPeg
  234.  
  235.     gosub [drawPuzzle]
  236.     if tiny = 3 and small = 3 and medium = 3 and large = 3 then [youWin]
  237.  
  238.     goto [inputLoop]
  239.  
  240.  
  241. [illegalMove]
  242.  
  243.     notice "That move is not allowed!"
  244.  
  245.  
  246. [resetSelection]
  247.  
  248.     state$ = "begin"
  249.     print #hanoi, "place 10 10 ; color white" ;
  250.     print #hanoi, "\\ Peg "; peg; " has been selected.\ Please select a destination peg." ;
  251.     goto [inputLoop]
  252.  
  253.  
  254. [youWin]
  255.  
  256.     beep
  257.     notice "You win!"
  258.     confirm "Play another?"; answer$
  259.     if answer$ = "no" then [quitHanoi]
  260.  
  261.     tiny = 1
  262.     small = 1
  263.     medium = 1
  264.     large = 1
  265.     gosub [drawPuzzle]
  266.     goto [inputLoop]
  267.  
  268.  
  269. [quitHanoi]
  270.  
  271.     close #hanoi
  272.     end
  273.